package com.shujia.day19.mysql.pool;

import java.io.FileReader;
import java.io.IOException;
import java.util.Properties;

public class MysqlPoolConfig {
    /*
        TODO 该类可以读取一些相关的配置信息，并且可以通过该类创建Mysql的连接对象
     */
    // 分析：1.需要读取配置文件
    String configPath;
    MysqlConfig mysqlConfig;

    int init_con_num;
    int min_con_num;
    int max_con_num;

    // 检查所使用的配置信息
    String unit;
    long kill_time;
    long delay_time;
    long interval_time;

    private MysqlPoolConfig() {
        // TODO 没有提供配置文件路径，使用默认的配置信息
        configPath = null;
        mysqlConfig = new MysqlConfig("jdbc:mysql://localhost:3306/bigdata_30","root","123456","com.mysql.jdbc.Driver");
        init_con_num = 1;
        min_con_num = 1;
        max_con_num = 2;
    }

    public MysqlPoolConfig(String configPath) throws Exception {
        this();
        this.configPath = configPath;
        Properties properties = new Properties();
        try {
            properties.load(new FileReader(configPath));
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        String[] allPar ={"driver","url","user","passwd"};
        for (String key : allPar) {
            if (!properties.containsKey(key)) {
                throw new Exception(key+"关键配置缺失..");
            }
        }

        // 获取Mysql连接信息
        this.mysqlConfig.driver = properties.getProperty("driver");
        this.mysqlConfig.url = properties.getProperty("url");
        this.mysqlConfig.user = properties.getProperty("user");
        this.mysqlConfig.passwd = properties.getProperty("passwd");

        // 获取连接数信息
        if (properties.containsKey("init_con_num")){
            init_con_num = Integer.valueOf(properties.getProperty("init_con_num"));
        }
        if (properties.containsKey("min_con_num")){
            min_con_num = Integer.valueOf(properties.getProperty("min_con_num"));
        }
        if (properties.containsKey("max_con_num")){
            max_con_num = Integer.valueOf(properties.getProperty("max_con_num"));
        }

        // 获取检查配置信息
        if (properties.containsKey("unit")){
            unit = properties.getProperty("unit");
        }
        if (properties.containsKey("kill_time")){
            if ("S".equals(unit)){
                kill_time = Integer.valueOf(properties.getProperty("kill_time")) * 1000;
            }
            // ...
        }
        if (properties.containsKey("delay_time")){
            if ("S".equals(unit)){
                delay_time = Integer.valueOf(properties.getProperty("delay_time")) * 1000;
            }
        }
        if (properties.containsKey("interval_time")){
            if ("S".equals(unit)){
                interval_time = Integer.valueOf(properties.getProperty("interval_time")) * 1000;
            }
        }


    }

    public String getConfigPath() {
        return configPath;
    }

    public void setConfigPath(String configPath) {
        this.configPath = configPath;
    }


    public int getInit_con_num() {
        return init_con_num;
    }

    public void setInit_con_num(int init_con_num) {
        this.init_con_num = init_con_num;
    }

    public int getMin_con_num() {
        return min_con_num;
    }

    public void setMin_con_num(int min_con_num) {
        this.min_con_num = min_con_num;
    }

    public int getMax_con_num() {
        return max_con_num;
    }

    public void setMax_con_num(int max_con_num) {
        this.max_con_num = max_con_num;
    }



    static class MysqlConfig{
        String url;
        String user;
        String passwd;
        String driver;

        public MysqlConfig(String url, String user, String passwd, String driver) {
            this.url = url;
            this.user = user;
            this.passwd = passwd;
            this.driver = driver;
        }
    }
}
